using UnityEngine;
using UnityEditor;
using System.Collections;

[CustomEditor(typeof(Material))]
[CanEditMultipleObjects]
public class FX_Uber_UnitShaderGUI : ShaderGUI
{
    // 选择列表
    private static readonly string[] uvModeOptions = new string[] {
        "UV1", "UV2", "Screen", "Polar", "New"
    };

    private static readonly string[] mixModeOptions = new string[] {
        "OFF", "Additive", "Mul", "Replace", "Remap", "Chorme"
    };

    private static readonly string[] maskModeOptions = new string[] {
        "Off", "ON"
    };

    private static readonly string[] distortModeOptions = new string[] {
        "Off", "ON"
    };

    // 渲染模式选择
    private static readonly string[] blendModeOptions = new string[] {
        "Alpha Blend", "Additive", "Mul"
    };

    // 渲染面选择
    private static readonly string[] renderFaceOptions = new string[] {
        "Back", "Front", "Both"
    };

    // 深度写入选择，对应 Unity 的 On Off 顺序
    private static readonly string[] depthWriteOptions = new string[] {
        "Off", "On"
    };


    // 材质属性
    private MaterialProperty uvMode;
    private MaterialProperty color;
    private MaterialProperty pow;
    private MaterialProperty AddMode;
    private MaterialProperty MulMode;    
    private MaterialProperty mainTex;
    private MaterialProperty mainColor;
    private MaterialProperty mainPow;
    private MaterialProperty mainUVTilling;
    private MaterialProperty mainUVPivot;
    private MaterialProperty mainUVSpeed;
    private MaterialProperty mainDistortInt;
    private MaterialProperty mainAlphaToColor;
    private MaterialProperty mainClampU;
    private MaterialProperty mainClampV;
    private MaterialProperty mainCustomDataInt;
    private MaterialProperty mixMode;
    private MaterialProperty secTex;
    private MaterialProperty secColor;
    private MaterialProperty secRGBLerp;
    private MaterialProperty secAlphaLerp;
    private MaterialProperty secPow;
    private MaterialProperty secUVTilling;
    private MaterialProperty secUVPivot;
    private MaterialProperty secUVSpeed;
    private MaterialProperty secDistortInt;
    private MaterialProperty secClampU;
    private MaterialProperty secClampV;
    private MaterialProperty secAlphaToColor;
    private MaterialProperty blendAlphaSwith;
    private MaterialProperty secCustomDataInt;
    private MaterialProperty chromeInt;
    private MaterialProperty chromeDir;
    private MaterialProperty chromeSuperAdj;
    private MaterialProperty chromeSuperLerp;
    private MaterialProperty maskMode;
    private MaterialProperty maskTex;
    private MaterialProperty maskPow;
    private MaterialProperty maskTilling;
    private MaterialProperty maskUVPivot;
    private MaterialProperty maskUVSpeed;
    private MaterialProperty maskDistortInt;
    private MaterialProperty maskClampU;
    private MaterialProperty maskClampV;
    private MaterialProperty maskAlphaToColor;
    private MaterialProperty maskCustomDataInt;
    private MaterialProperty distortMode;
    private MaterialProperty distortTex;
    private MaterialProperty distortTillSpeed;
    private MaterialProperty distortPow;
    private MaterialProperty fresnel;
    private MaterialProperty colorRim;
    private MaterialProperty colorInside;
    private MaterialProperty fresnelPow;

    // 新增属性
    private MaterialProperty softParticle;
    private MaterialProperty colorFlip;
    private MaterialProperty alphaSoft;

    // 渲染相关属性
    private MaterialProperty blendMode;  // 对应 _Blend
    private MaterialProperty renderFace; // 对应 _Cull
    private MaterialProperty depthWrite; // 对应 _ZWrite
    private MaterialProperty srcBlend;
    private MaterialProperty dstBlend;

    // 特效混合模式的临时存储
    private int localEffectBlendMode = 0;

    // 折叠状态
    private bool baseSettingsFoldout = true;
    private bool mainTextureFoldout = true;
    private bool mainUVSettingsFoldout = true; // 主纹理 UV 设置折叠状态
    private bool mainAdvancedFoldout = false;
    private bool mixModeFoldout = true;
    private bool mixUVSettingsFoldout = false; // 混合纹理 UV 设置折叠状态
    private bool mixAdvancedFoldout = false;
    private bool maskFoldout = false;
    private bool maskUVSettingsFoldout = false; // 遮罩纹理 UV 设置折叠状态
    private bool maskAdvancedFoldout = false;
    private bool distortFoldout = false;
    private bool fresnelFoldout = false;

    // 新增：特效按钮点击状态
    private bool showMixOptions = false;
    private bool showMaskOptions = false;
    private bool showDistortOptions = false;
    private bool showFresnelOptions = false;

    // 样式设置
    private GUIStyle boldFoldoutStyle;
    private GUIStyle miniBoldLabelStyle;
    private GUIStyle sectionBoxStyle;
    private GUIStyle advancedFoldoutStyle;
    private GUIStyle textureGroupStyle;
    private GUIStyle advancedContentStyle;
    private GUIStyle categoryHeaderStyle;
    private GUIStyle advancedHeaderStyle;
    private GUIStyle titleBoxStyle;
    private GUIStyle uvSettingsFoldoutStyle; // UV 设置折叠样式
    private GUIStyle uvModeLabelStyle; // UV 模式标签样式
    private GUIStyle smallLabelStyle; // 小标签样式
    private GUIStyle brightButtonStyle; // 亮色按钮样式
    private GUIStyle effectButtonStyle; // 特效按钮样式
    private GUIStyle effectRowStyle; // 特效功能行样式

    // 渐变纹理
    private Texture2D categoryHeaderGradient;
    private Texture2D advancedHeaderGradient;
    private Texture2D uvSettingsGradient; // UV 设置渐变纹理
    private Texture2D effectRowGradient; // 特效功能行渐变纹理

    public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
    {
        // 初始化样式
        InitializeStyles();

        // 查找材质属性
        FindProperties(properties);

        // 获取当前编辑的所有材质
        Material[] materials = new Material[materialEditor.targets.Length];
        for (int i = 0; i < materialEditor.targets.Length; i++)
        {
            materials[i] = materialEditor.targets[i] as Material;
        }

        // 开始 GUI 更改检查
        EditorGUI.BeginChangeCheck();

        // 绘制标题和折叠按钮
        DrawTitleAndFoldButtons();

        // 显示当前 UV 模式和渲染模式以及系统时间
        DrawCurrentUVAndBlendMode();

        // 新增：绘制特效功能行
        DrawEffectRow();

        // 绘制基础设置折叠项
        DrawFoldoutWithGradientBackground(ref baseSettingsFoldout, "基础设置", boldFoldoutStyle, categoryHeaderGradient);
        if (baseSettingsFoldout)
        {
            using (new EditorGUILayout.VerticalScope(sectionBoxStyle))
            {
                DrawEnumPopup(materialEditor, blendMode, "渲染模式", blendModeOptions);
                DrawEnumPopup(materialEditor, uvMode, "UV模式", uvModeOptions);
                DrawEnumPopup(materialEditor, renderFace, "渲染面", renderFaceOptions);
                DrawEnumPopup(materialEditor, depthWrite, "深度写入", depthWriteOptions);
                materialEditor.ShaderProperty(color, "颜色");
                materialEditor.ShaderProperty(pow, "透明度");
                materialEditor.ShaderProperty(softParticle, "软粒子");
                materialEditor.ShaderProperty(colorFlip, "颜色反转");
                materialEditor.ShaderProperty(alphaSoft, "透明区域羽化");
            }
        }
        EditorGUILayout.Space();

        // 绘制主纹理设置折叠项
        DrawFoldoutWithGradientBackground(ref mainTextureFoldout, "主纹理设置", boldFoldoutStyle, categoryHeaderGradient);
        if (mainTextureFoldout && mainTex != null && mainColor != null && mainPow != null)
        {
            using (new EditorGUILayout.VerticalScope(sectionBoxStyle))
            {
                // 主纹理 UV 坐标、颜色选择
                EditorGUILayout.LabelField("主纹理", EditorStyles.boldLabel);
                using (new EditorGUILayout.VerticalScope(textureGroupStyle))
                {
                    materialEditor.TexturePropertySingleLine(new GUIContent("纹理"), mainTex, mainColor);
                    materialEditor.ShaderProperty(mainPow, "纹理透明度");
                    materialEditor.ShaderProperty(mainUVTilling, "UV平铺偏移");

                    // 主纹理 UV 设置折叠项
                    DrawFoldoutWithGradientBackground(ref mainUVSettingsFoldout, "UV流动设置", uvSettingsFoldoutStyle, uvSettingsGradient);
                    if (mainUVSettingsFoldout && mainUVTilling != null && mainUVSpeed != null)
                    {
                        using (new EditorGUILayout.VerticalScope(advancedContentStyle))
                        {
                            // 获取当前速度值
                            Vector4 mainUVSpeed4 = mainUVSpeed.vectorValue;
                            
                            // 获取材质的唯一标识符，用于存储偏好设置
                            string materialId = materialEditor.target.name;
                            
                            // 从EditorPrefs获取保存的滑块范围值，如果不存在则使用默认值1
                            string prefsKey = $"UVSliderRange_{materialId}";
                            float sliderRange = EditorPrefs.GetFloat(prefsKey, 1f);
                            
                            // 显示并更新滑块范围控制
                            sliderRange = EditorGUILayout.Slider("滑块范围调整", sliderRange, 0.1f, 10f);
                            
                            // 保存滑块范围值到EditorPrefs
                            EditorPrefs.SetFloat(prefsKey, sliderRange);
                            
                            // 显示并更新速度控制滑块 (范围由sliderRange决定)
                            mainUVSpeed4.x = EditorGUILayout.Slider("水平速度", mainUVSpeed4.x, -sliderRange, sliderRange);
                            mainUVSpeed4.y = EditorGUILayout.Slider("垂直速度", mainUVSpeed4.y, -sliderRange, sliderRange);
                            mainUVSpeed4.z = EditorGUILayout.Slider("旋转速度", mainUVSpeed4.z, -sliderRange, sliderRange);
                            mainUVSpeed4.w = EditorGUILayout.Slider("旋转角度", mainUVSpeed4.w, -sliderRange, sliderRange);

                            // 应用更新后的速度值
                            mainUVSpeed.vectorValue = mainUVSpeed4;
                        }
                    }

                    // 高级设置折叠项
                    DrawFoldoutWithGradientBackground(ref mainAdvancedFoldout, "高级设置", advancedFoldoutStyle, advancedHeaderGradient);
                    if (mainAdvancedFoldout && mainDistortInt != null)
                    {
                        using (new EditorGUILayout.VerticalScope(advancedContentStyle))
                        {
                            EditorGUILayout.LabelField("扭曲设置", miniBoldLabelStyle);
                            materialEditor.ShaderProperty(mainDistortInt, "扭曲强度");
                            if (mainAlphaToColor != null) materialEditor.ShaderProperty(mainAlphaToColor, "Alpha转颜色");

                            if (mainClampU != null && mainClampV != null && mainCustomDataInt != null)
                            {
                                EditorGUILayout.LabelField("边界限制", miniBoldLabelStyle);
                                materialEditor.ShaderProperty(mainClampU, "限制U范围");
                                materialEditor.ShaderProperty(mainClampV, "限制V范围");
                                materialEditor.ShaderProperty(mainCustomDataInt, "自定义数据影响");
                            }
                        }
                    }
                }
            }
        }
        EditorGUILayout.Space();

        // 新增：根据按钮状态绘制混合模式设置折叠项
        if (showMixOptions)
        {
            foreach (Material material in materials)
            {
                mixMode.floatValue = 1; // OFF
            }
            DrawFoldoutWithGradientBackground(ref mixModeFoldout, "混合模式设置", boldFoldoutStyle, categoryHeaderGradient);
            if (mixModeFoldout && mixMode != null)
            {
                using (new EditorGUILayout.VerticalScope(sectionBoxStyle))
                {
                    DrawEnumPopup(materialEditor, mixMode, "混合模式", mixModeOptions);

                    if ((int)mixMode.floatValue != 0 && secTex != null && secColor != null && secPow != null)
                    {
                        // 混合纹理 UV 坐标、颜色选择
                        EditorGUILayout.LabelField("混合纹理", EditorStyles.boldLabel);
                        using (new EditorGUILayout.VerticalScope(textureGroupStyle))
                        {
                            materialEditor.TexturePropertySingleLine(new GUIContent("纹理"), secTex, secColor);
                            if (secRGBLerp != null) materialEditor.ShaderProperty(secRGBLerp, "RGB插值");
                            if (secAlphaLerp != null) materialEditor.ShaderProperty(secAlphaLerp, "Alpha插值");
                            materialEditor.ShaderProperty(secPow, "纹理透明度");
                            materialEditor.ShaderProperty(secUVTilling, "UV平铺偏移");

                            // 混合纹理 UV 设置折叠项
                            DrawFoldoutWithGradientBackground(ref mixUVSettingsFoldout, "UV设置", uvSettingsFoldoutStyle, uvSettingsGradient);
                            if (mixUVSettingsFoldout && secUVTilling != null && secUVSpeed != null)
                            {
                                using (new EditorGUILayout.VerticalScope(advancedContentStyle))
                                {

                                     // 获取当前速度值
                                    Vector4 secUVSpeed4 = secUVSpeed.vectorValue;
                                    
                                    // 获取材质的唯一标识符，用于存储偏好设置
                                    string secmaterialId = materialEditor.target.name;
                                    
                                    // 从EditorPrefs获取保存的滑块范围值，如果不存在则使用默认值1
                                    string secprefsKey = $"UVSliderRangesec_{secmaterialId}";
                                    float secsliderRange = EditorPrefs.GetFloat(secprefsKey, 1f);
                                    
                                    // 显示并更新滑块范围控制
                                    secsliderRange = EditorGUILayout.Slider("滑块范围调整", secsliderRange, 0.1f, 10f);
                                    
                                    // 保存滑块范围值到EditorPrefs
                                    EditorPrefs.SetFloat(secprefsKey, secsliderRange);
                                    
                                    // 显示并更新速度控制滑块 (范围由sliderRange决定)
                                    secUVSpeed4.x = EditorGUILayout.Slider("水平速度", secUVSpeed4.x, -secsliderRange, secsliderRange);
                                    secUVSpeed4.y = EditorGUILayout.Slider("垂直速度", secUVSpeed4.y, -secsliderRange, secsliderRange);
                                    secUVSpeed4.z = EditorGUILayout.Slider("旋转速度", secUVSpeed4.z, -secsliderRange, secsliderRange);
                                    secUVSpeed4.w = EditorGUILayout.Slider("旋转角度", secUVSpeed4.w, -secsliderRange, secsliderRange);

                                    // 应用更新后的速度值
                                    secUVSpeed.vectorValue = secUVSpeed4;
                                }
                            }

                            // 高级设置折叠项
                            DrawFoldoutWithGradientBackground(ref mixAdvancedFoldout, "高级设置", advancedFoldoutStyle, advancedHeaderGradient);
                            if (mixAdvancedFoldout && secDistortInt != null)
                            {
                                using (new EditorGUILayout.VerticalScope(advancedContentStyle))
                                {
                                    EditorGUILayout.LabelField("扭曲设置", miniBoldLabelStyle);
                                    materialEditor.ShaderProperty(secDistortInt, "扭曲强度");
                                    if (secClampU != null) materialEditor.ShaderProperty(secClampU, "限制U范围");
                                    if (secClampV != null) materialEditor.ShaderProperty(secClampV, "限制V范围");
                                    if (secAlphaToColor != null) materialEditor.ShaderProperty(secAlphaToColor, "Alpha转颜色");
                                    if (blendAlphaSwith != null) materialEditor.ShaderProperty(blendAlphaSwith, "混合Alpha开关");
                                    if (secCustomDataInt != null) materialEditor.ShaderProperty(secCustomDataInt, "自定义数据影响");

                                    if ((int)mixMode.floatValue == 5 && chromeInt != null)
                                    {
                                        EditorGUILayout.LabelField("颜色校正", miniBoldLabelStyle);
                                        materialEditor.ShaderProperty(chromeInt, "颜色强度");
                                        if (chromeDir != null) materialEditor.ShaderProperty(chromeDir, "颜色方向");
                                        if (chromeSuperAdj != null) materialEditor.ShaderProperty(chromeSuperAdj, "高亮颜色偏移值");
                                        if (chromeSuperLerp != null) materialEditor.ShaderProperty(chromeSuperLerp, "高亮颜色与普通颜色插值");
                                    }
                                }
                            }
                        }
                    }
                }
            }
            EditorGUILayout.Space();
        }
        else
        {
            // 设置混合模式为 OFF (索引0)
            foreach (Material material in materials)
            {
                mixMode.floatValue = 0; // OFF
            }
        }

        // 新增：根据按钮状态绘制遮罩设置折叠项
        if (showMaskOptions)
        {
            // 设置遮罩模式为 On
            foreach (Material material in materials)
            {
                maskMode.floatValue = 1; 
            }
            DrawFoldoutWithGradientBackground(ref maskFoldout, "遮罩设置", boldFoldoutStyle, categoryHeaderGradient);
            if (maskFoldout && maskMode != null)
            {
                using (new EditorGUILayout.VerticalScope(sectionBoxStyle))
                {


                    if ((int)maskMode.floatValue == 1 && maskTex != null && maskPow != null)
                    {
                        // 遮罩纹理 UV 坐标、颜色选择
                        EditorGUILayout.LabelField("遮罩纹理", EditorStyles.boldLabel);
                        using (new EditorGUILayout.VerticalScope(textureGroupStyle))
                        {
                            materialEditor.TexturePropertySingleLine(new GUIContent("纹理"), maskTex);
                            materialEditor.ShaderProperty(maskPow, "遮罩强度");
                            materialEditor.ShaderProperty(maskTilling, "UV平铺偏移");

                            // 遮罩纹理 UV 设置折叠项
                            DrawFoldoutWithGradientBackground(ref maskUVSettingsFoldout, "UV设置", uvSettingsFoldoutStyle, uvSettingsGradient);
                            if (maskUVSettingsFoldout && maskTilling != null && maskUVSpeed != null)
                            {
                                using (new EditorGUILayout.VerticalScope(advancedContentStyle))
                                {
                                    // 获取当前速度值
                                    Vector4 maskUVSpeed4 = maskUVSpeed.vectorValue;
                                    
                                    // 获取材质的唯一标识符，用于存储偏好设置
                                    string maskmaterialId = materialEditor.target.name;
                                    
                                    // 从EditorPrefs获取保存的滑块范围值，如果不存在则使用默认值1
                                    string maskprefsKey = $"UVSliderRange2_{maskmaterialId}";
                                    float masksliderRange = EditorPrefs.GetFloat(maskprefsKey, 1f);
                                    
                                    // 显示并更新滑块范围控制
                                    masksliderRange = EditorGUILayout.Slider("滑块范围调整", masksliderRange, 0.1f, 10f);
                                    
                                    // 保存滑块范围值到EditorPrefs
                                    EditorPrefs.SetFloat(maskprefsKey, masksliderRange);
                                    
                                    // 显示并更新速度控制滑块 (范围由sliderRange决定)
                                    maskUVSpeed4.x = EditorGUILayout.Slider("水平速度", maskUVSpeed4.x, -masksliderRange, masksliderRange);
                                    maskUVSpeed4.y = EditorGUILayout.Slider("垂直速度", maskUVSpeed4.y, -masksliderRange, masksliderRange);
                                    maskUVSpeed4.z = EditorGUILayout.Slider("旋转速度", maskUVSpeed4.z, -masksliderRange, masksliderRange);
                                    maskUVSpeed4.w = EditorGUILayout.Slider("旋转角度", maskUVSpeed4.w, -masksliderRange, masksliderRange);

                                    // 应用更新后的速度值
                                    maskUVSpeed.vectorValue = maskUVSpeed4;
                                }
                            }

                            // 高级设置折叠项
                            DrawFoldoutWithGradientBackground(ref maskAdvancedFoldout, "高级设置", advancedFoldoutStyle, advancedHeaderGradient);
                            if (maskAdvancedFoldout && maskDistortInt != null)
                            {
                                using (new EditorGUILayout.VerticalScope(advancedContentStyle))
                                {
                                    EditorGUILayout.LabelField("扭曲设置", miniBoldLabelStyle);
                                    materialEditor.ShaderProperty(maskDistortInt, "扭曲强度");
                                    if (maskClampU != null) materialEditor.ShaderProperty(maskClampU, "限制U范围");
                                    if (maskClampV != null) materialEditor.ShaderProperty(maskClampV, "限制V范围");
                                    if (maskAlphaToColor != null) materialEditor.ShaderProperty(maskAlphaToColor, "Alpha转颜色");
                                    if (maskCustomDataInt != null) materialEditor.ShaderProperty(maskCustomDataInt, "自定义数据影响");
                                }
                            }
                        }
                    }
                }
            }
            EditorGUILayout.Space();
        }
        else
        {
            // 设置遮罩模式为 OFF (索引0)
            foreach (Material material in materials)
            {
                maskMode.floatValue = 0; // OFF
            }
        }
        // 新增：根据按钮状态绘制扭曲效果设置折叠项
        if (showDistortOptions)
        {
            foreach (Material material in materials)
            {
                distortMode.floatValue = 1; // OFF
            }
            DrawFoldoutWithGradientBackground(ref distortFoldout, "扭曲效果设置", boldFoldoutStyle, categoryHeaderGradient);
            if (distortFoldout && distortMode != null)
            {
                using (new EditorGUILayout.VerticalScope(sectionBoxStyle))
                {

                    if ((int)distortMode.floatValue == 1 && distortTex != null && distortTillSpeed != null && distortPow != null)
                    {
                        using (new EditorGUILayout.VerticalScope(textureGroupStyle))
                        {
                            materialEditor.TexturePropertySingleLine(new GUIContent("扭曲纹理"), distortTex);
                            materialEditor.ShaderProperty(distortTillSpeed, "扭曲UV速度");
                            materialEditor.ShaderProperty(distortPow, "扭曲强度");
                        }
                    }
                }
            }
            EditorGUILayout.Space();
        }
        else
        {
            // 设置扭曲模式为 OFF (索引0)
            foreach (Material material in materials)
            {
                distortMode.floatValue = 0; // OFF
            }
        }
        // 新增：根据按钮状态绘制菲涅尔效果设置折叠项
        if (showFresnelOptions)
        {
            foreach (Material material in materials)
            {
                fresnel.floatValue = 1;
            }        
            DrawFoldoutWithGradientBackground(ref fresnelFoldout, "菲涅尔效果设置", boldFoldoutStyle, categoryHeaderGradient);
            if (fresnelFoldout && fresnel != null)
            {
                using (new EditorGUILayout.VerticalScope(sectionBoxStyle))
                {

                    if (fresnel.floatValue > 0 && colorRim != null && colorInside != null)
                    {
                        using (new EditorGUILayout.VerticalScope(textureGroupStyle))
                        {
                            materialEditor.ShaderProperty(colorRim, "边缘颜色");
                            materialEditor.ShaderProperty(colorInside, "内部颜色");
                            if (fresnelPow != null) materialEditor.ShaderProperty(fresnelPow, "菲涅尔幂值");
                        }
                    }
                }
            }
        }
        else
        {
            // 设置菲涅尔强度为 0
            foreach (Material material in materials)
            {
                fresnel.floatValue = 0;
            }
        }
        // 应用更改
        if (EditorGUI.EndChangeCheck())
        {
            // 更新所有材质的关键字
            foreach (Material material in materials)
            {
                UpdateMaterialKeywords(material);
            }

            // 渲染模式更改时确保使用正确的混合模式
            foreach (Material material in materials)
            {
                UpdateMaterialBlendMode(material);
            }

            materialEditor.PropertiesChanged();
        }
    }

    // 绘制标题和折叠按钮
    private void DrawTitleAndFoldButtons()
    {
        using (new EditorGUILayout.HorizontalScope(titleBoxStyle))
        {
            EditorGUILayout.LabelField("特效UNIT着色器", EditorStyles.boldLabel);

            GUILayout.FlexibleSpace();

            GUI.color = AreAllBaseFoldoutsOpen() ? Color.white : new Color(0.7f, 0.7f, 0.7f);
            if (GUILayout.Button("基础设置展开", brightButtonStyle))
            {
                bool allOpen = mainUVSettingsFoldout && mixUVSettingsFoldout && maskUVSettingsFoldout;
                mainUVSettingsFoldout = !allOpen;
                mixUVSettingsFoldout = !allOpen;
                maskUVSettingsFoldout = !allOpen;
            }

            GUI.color = AreAllAdvancedFoldoutsOpen() ? Color.white : new Color(0.7f, 0.7f, 0.7f);
            if (GUILayout.Button("专业设置展开", brightButtonStyle))
            {
                bool allOpen = mainAdvancedFoldout && mixAdvancedFoldout && maskAdvancedFoldout;
                mainAdvancedFoldout = !allOpen;
                mixAdvancedFoldout = !allOpen;
                maskAdvancedFoldout = !allOpen;
            }
            GUI.color = Color.white;
        }
    }

    // 检查所有基础折叠项是否展开
    private bool AreAllBaseFoldoutsOpen()
    {
        return baseSettingsFoldout && mainTextureFoldout && mixModeFoldout && maskFoldout && distortFoldout && fresnelFoldout &&
               mainUVSettingsFoldout && mixUVSettingsFoldout && maskUVSettingsFoldout;
    }

    // 检查所有高级折叠项是否展开
    private bool AreAllAdvancedFoldoutsOpen()
    {
        return mainAdvancedFoldout && mixAdvancedFoldout && maskAdvancedFoldout;
    }

    // 显示当前 UV 模式和渲染模式以及系统时间
    private void DrawCurrentUVAndBlendMode()
    {
        string currentUVMode = uvMode != null ? uvModeOptions[(int)uvMode.floatValue] : "未知";
        string currentBlendMode = blendMode != null ? blendModeOptions[(int)blendMode.floatValue] : "未知";
        string currentTime = System.DateTime.Now.ToString("HH:mm:ss");
        EditorGUILayout.LabelField($"  当前渲染模式: {currentBlendMode} 当前UV模式: {currentUVMode}    当前时间: {currentTime}", smallLabelStyle);
    }

    // 绘制带渐变背景的折叠项
    private void DrawFoldoutWithGradientBackground(ref bool foldout, string title, GUIStyle style, Texture2D gradientTexture)
    {
        Rect rect = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight);

        // 绘制渐变背景
        if (gradientTexture != null)
        {
            EditorGUI.DrawPreviewTexture(rect, gradientTexture);
        }

        // 绘制折叠按钮
        foldout = EditorGUI.Foldout(rect, foldout, title, true, style);
    }

    // 绘制枚举下拉框
    private void DrawEnumPopup(MaterialEditor materialEditor, MaterialProperty property, string label, string[] options)
    {
        if (property == null)
        {
            EditorGUILayout.HelpBox($"属性 '{label}' 不存在于当前 shader 中", MessageType.Warning);
            return;
        }

        EditorGUI.showMixedValue = property.hasMixedValue;
        EditorGUI.BeginChangeCheck();

        int index = (int)property.floatValue;
        index = EditorGUILayout.Popup(label, index, options);

        if (EditorGUI.EndChangeCheck())
        {
            property.floatValue = index;
        }
        EditorGUI.showMixedValue = false;
    }

    // 初始化自定义样式
    private void InitializeStyles()
    {
        if (boldFoldoutStyle == null)
        {
            boldFoldoutStyle = new GUIStyle(EditorStyles.foldout);
            boldFoldoutStyle.fontStyle = FontStyle.Bold;
            boldFoldoutStyle.fontSize = 12;
            boldFoldoutStyle.margin = new RectOffset(10, 0, 0, 0);
        }

        if (advancedFoldoutStyle == null)
        {
            advancedFoldoutStyle = new GUIStyle(EditorStyles.foldout);
            advancedFoldoutStyle.fontStyle = FontStyle.Normal;
            advancedFoldoutStyle.fontSize = 11;
            advancedFoldoutStyle.margin = new RectOffset(20, 0, 0, 0);
        }

        if (uvSettingsFoldoutStyle == null)
        {
            uvSettingsFoldoutStyle = new GUIStyle(EditorStyles.foldout);
            uvSettingsFoldoutStyle.fontStyle = FontStyle.Normal;
            uvSettingsFoldoutStyle.fontSize = 11;
            uvSettingsFoldoutStyle.margin = new RectOffset(20, 0, 0, 0);
        }

        if (miniBoldLabelStyle == null)
        {
            miniBoldLabelStyle = new GUIStyle(EditorStyles.miniBoldLabel);
            miniBoldLabelStyle.margin = new RectOffset(15, 0, 5, 5);
        }

        if (sectionBoxStyle == null)
        {
            sectionBoxStyle = new GUIStyle(GUI.skin.box);
            sectionBoxStyle.margin = new RectOffset(10, 10, 5, 5);
            sectionBoxStyle.padding = new RectOffset(10, 10, 10, 10);
            sectionBoxStyle.border = new RectOffset(1, 1, 1, 1);
        }

        if (textureGroupStyle == null)
        {
            textureGroupStyle = new GUIStyle(GUI.skin.box);
            textureGroupStyle.margin = new RectOffset(5, 5, 5, 5);
            textureGroupStyle.padding = new RectOffset(10, 10, 10, 10);
            textureGroupStyle.border = new RectOffset(1, 1, 1, 1);
            textureGroupStyle.normal.background = CreateColoredTexture(new Color(0.15f, 0.15f, 0.15f, 0.2f));
        }

        if (advancedContentStyle == null)
        {
            advancedContentStyle = new GUIStyle(GUI.skin.box);
            advancedContentStyle.margin = new RectOffset(15, 15, 5, 5);
            advancedContentStyle.padding = new RectOffset(10, 10, 10, 10);
            advancedContentStyle.border = new RectOffset(1, 1, 1, 1);
            advancedContentStyle.normal.background = CreateColoredTexture(new Color(0.1f, 0.1f, 0.1f, 0.2f));
        }

        if (categoryHeaderStyle == null)
        {
            categoryHeaderStyle = new GUIStyle();
            // 此处不需要设置样式因为已经有渐变背景
        }

        if (advancedHeaderStyle == null)
        {
            advancedHeaderStyle = new GUIStyle();
            // 此处不需要设置样式因为已经有渐变背景
        }

        if (titleBoxStyle == null)
        {
            titleBoxStyle = new GUIStyle(GUI.skin.box);
            titleBoxStyle.normal.background = CreateColoredTexture(Color.black);
            titleBoxStyle.padding = new RectOffset(10, 10, 5, 5);
        }

        if (uvModeLabelStyle == null)
        {
            uvModeLabelStyle = new GUIStyle(EditorStyles.label);
            uvModeLabelStyle.fontStyle = FontStyle.Bold;
            uvModeLabelStyle.normal.textColor = Color.white;
            uvModeLabelStyle.margin = new RectOffset(10, 10, 0, 0);
        }

        if (smallLabelStyle == null)
        {
            smallLabelStyle = new GUIStyle(EditorStyles.label);
            smallLabelStyle.fontSize = 10;
            smallLabelStyle.alignment = TextAnchor.MiddleLeft;
        }

        if (brightButtonStyle == null)
        {
            brightButtonStyle = new GUIStyle(GUI.skin.button);
            brightButtonStyle.normal.textColor = Color.white;
        }

        // 新增：特效按钮样式
        if (effectButtonStyle == null)
        {
            effectButtonStyle = new GUIStyle(GUI.skin.button);
            effectButtonStyle.normal.textColor = Color.white;
        }

        // 新增：特效功能行样式
        if (effectRowStyle == null)
        {
            effectRowStyle = new GUIStyle(GUI.skin.box);
            effectRowStyle.normal.background = CreateColoredTexture(new Color(0.1f, 0.1f, 0.1f, 1f));
            effectRowStyle.padding = new RectOffset(10, 10, 5, 5);
        }

        // 初始化渐变纹理
        if (categoryHeaderGradient == null)
        {
            // 类别标题的渐变 - 灰色
            categoryHeaderGradient = CreateGradientTexture(256, 1, new Color(0.2f, 0.2f, 0.2f, 0.3f), new Color(0.1f, 0.1f, 0.1f, 0.4f));
        }

        if (advancedHeaderGradient == null)
        {
            // 高级设置标题的渐变 - 深灰色
            advancedHeaderGradient = CreateGradientTexture(256, 1, new Color(0.15f, 0.15f, 0.15f, 0.4f), new Color(0.05f, 0.05f, 0.05f, 0.5f));
        }

        if (uvSettingsGradient == null)
        {
            // UV 设置标题的渐变 - 比 textureGroupStyle 颜色变化
            uvSettingsGradient = CreateGradientTexture(256, 1, new Color(0.12f, 0.12f, 0.12f, 0.3f), new Color(0.08f, 0.08f, 0.08f, 0.4f));
        }

        // 新增：特效功能行渐变纹理
        if (effectRowGradient == null)
        {
            effectRowGradient = CreateColoredTexture(new Color(0.1f, 0.1f, 0.1f, 1f));
        }
    }

    // 创建纯色纹理
    private Texture2D CreateColoredTexture(Color color)
    {
        Texture2D tex = new Texture2D(1, 1);
        tex.SetPixel(0, 0, color);
        tex.Apply();
        return tex;
    }

    // 创建水平渐变纹理
    private Texture2D CreateGradientTexture(int width, int height, Color leftColor, Color rightColor)
    {
        Texture2D tex = new Texture2D(width, height);

        for (int x = 0; x < width; x++)
        {
            float t = (float)x / (width - 1);
            Color color = Color.Lerp(leftColor, rightColor, t);

            for (int y = 0; y < height; y++)
            {
                tex.SetPixel(x, y, color);
            }
        }

        tex.Apply();
        return tex;
    }

    // 查找材质属性
    private void FindProperties(MaterialProperty[] properties)
    {
        uvMode = ShaderGUIUtil.FindProperty("_UVMODE", properties, false);
        color = ShaderGUIUtil.FindProperty("_Color", properties);
        pow = ShaderGUIUtil.FindProperty("_Pow", properties);
        AddMode = ShaderGUIUtil.FindProperty("_AddMode", properties);
        MulMode = ShaderGUIUtil.FindProperty("_MulMode", properties);
        mainTex = ShaderGUIUtil.FindProperty("_MainTex", properties);
        mainColor = ShaderGUIUtil.FindProperty("_MainColor", properties);
        mainPow = ShaderGUIUtil.FindProperty("_MainPow", properties);
        mainUVTilling = ShaderGUIUtil.FindProperty("_MainUVTilling", properties);
        mainUVPivot = ShaderGUIUtil.FindProperty("_MainUVPivot", properties);
        mainUVSpeed = ShaderGUIUtil.FindProperty("_MainUVSpeed", properties);
        mainDistortInt = ShaderGUIUtil.FindProperty("_MainDistortInt", properties);
        mainAlphaToColor = ShaderGUIUtil.FindProperty("_MainAlphaToColor", properties);
        mainClampU = ShaderGUIUtil.FindProperty("_MainClampU", properties);
        mainClampV = ShaderGUIUtil.FindProperty("_MainClampV", properties);
        mainCustomDataInt = ShaderGUIUtil.FindProperty("_MainCustomDataInt", properties);
        mixMode = ShaderGUIUtil.FindProperty("_MIXMODE", properties);
        secTex = ShaderGUIUtil.FindProperty("_SecTex", properties);
        secColor = ShaderGUIUtil.FindProperty("_SecColor", properties);
        secRGBLerp = ShaderGUIUtil.FindProperty("_SecRGBLerp", properties);
        secAlphaLerp = ShaderGUIUtil.FindProperty("_SecAlphaLerp", properties);
        secPow = ShaderGUIUtil.FindProperty("_SecPow", properties);
        secUVTilling = ShaderGUIUtil.FindProperty("_SecUVTilling", properties);
        secUVPivot = ShaderGUIUtil.FindProperty("_SecUVPivot", properties);
        secUVSpeed = ShaderGUIUtil.FindProperty("_SecUVSpeed", properties);
        secDistortInt = ShaderGUIUtil.FindProperty("_SecDistortInt", properties);
        secClampU = ShaderGUIUtil.FindProperty("_SecClampU", properties);
        secClampV = ShaderGUIUtil.FindProperty("_SecClampV", properties);
        secAlphaToColor = ShaderGUIUtil.FindProperty("_SecAlphaToColor", properties);
        blendAlphaSwith = ShaderGUIUtil.FindProperty("_BlendAlphaSwith", properties);
        secCustomDataInt = ShaderGUIUtil.FindProperty("_SecCustomDataInt", properties);
        chromeInt = ShaderGUIUtil.FindProperty("_ChromeInt", properties);
        chromeDir = ShaderGUIUtil.FindProperty("_ChromeDIr", properties);
        chromeSuperAdj = ShaderGUIUtil.FindProperty("_ChromeSuperAdj", properties);
        chromeSuperLerp = ShaderGUIUtil.FindProperty("_ChromeSuperLerp", properties);
        maskMode = ShaderGUIUtil.FindProperty("_MASKMODE", properties);
        maskTex = ShaderGUIUtil.FindProperty("_MaskTex", properties);
        maskPow = ShaderGUIUtil.FindProperty("_MaskPow", properties);
        maskTilling = ShaderGUIUtil.FindProperty("_MaskTilling", properties);
        maskUVPivot = ShaderGUIUtil.FindProperty("_MaskUVPivot", properties);
        maskUVSpeed = ShaderGUIUtil.FindProperty("_MaskUVSpeed", properties);
        maskDistortInt = ShaderGUIUtil.FindProperty("_MaskDistortInt", properties);
        maskClampU = ShaderGUIUtil.FindProperty("_MaskClampU", properties);
        maskClampV = ShaderGUIUtil.FindProperty("_MaskClampV", properties);
        maskAlphaToColor = ShaderGUIUtil.FindProperty("_MaskAlphaToColor", properties);
        maskCustomDataInt = ShaderGUIUtil.FindProperty("_MaskCustomDataInt", properties);
        distortMode = ShaderGUIUtil.FindProperty("_DISTORTMODE", properties);
        distortTex = ShaderGUIUtil.FindProperty("_DistortTex", properties);
        distortTillSpeed = ShaderGUIUtil.FindProperty("_DistortTillSpeed", properties);
        distortPow = ShaderGUIUtil.FindProperty("_DistortPow", properties);
        fresnel = ShaderGUIUtil.FindProperty("_Fresnel", properties);
        colorRim = ShaderGUIUtil.FindProperty("_ColorRim", properties);
        colorInside = ShaderGUIUtil.FindProperty("_ColorInside", properties);
        fresnelPow = ShaderGUIUtil.FindProperty("_FresnelPow", properties);
        softParticle = ShaderGUIUtil.FindProperty("_SoftParticle", properties);
        colorFlip = ShaderGUIUtil.FindProperty("_ColorFlip", properties);
        alphaSoft = ShaderGUIUtil.FindProperty("_AlphaSoft", properties);
        // shader里
        blendMode = ShaderGUIUtil.FindProperty("_Blend", properties);
        renderFace = ShaderGUIUtil.FindProperty("_Cull", properties);
        depthWrite = ShaderGUIUtil.FindProperty("_ZWrite", properties);
        srcBlend = ShaderGUIUtil.FindProperty("_SrcBlend", properties);
        dstBlend = ShaderGUIUtil.FindProperty("_DstBlend", properties);

    }

    // 更新材质的关键字
    private void UpdateMaterialKeywords(Material material)
    {
        // 确保关键字格式为 _SHADERNAME_ENUMVALUE
        // 例如 _UVMODE_UV1, _UVMODE_UV2, 等

        // 更新 UV 模式关键字
        ClearAllKeywords(material, "UVMODE");
        switch ((int)uvMode.floatValue)
        {
            case 0: material.EnableKeyword("_UVMODE_UV1"); break;
            // 其他 UV 模式关键字更新
            case 1: material.EnableKeyword("_UVMODE_UV2"); break;
            case 2: material.EnableKeyword("_UVMODE_Screen"); break;            
            case 3: material.EnableKeyword("_UVMODE_Polar"); break;
            case 4: material.EnableKeyword("_UVMODE_New"); break;
        }
    }

    // 清除指定前缀的所有关键字
    private void ClearAllKeywords(Material material, string prefix)
    {
        string[] keywords = material.shaderKeywords;
        for (int i = 0; i < keywords.Length; i++)
        {
            if (keywords[i].StartsWith(prefix))
            {
                material.DisableKeyword(keywords[i]);
            }
        }
    }

    // 更新材质的混合模式
    private void UpdateMaterialBlendMode(Material material)
    {


        switch ((int)blendMode.floatValue)
        {
            case 0: // Alpha Blend
                material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
                material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                AddMode.floatValue = 0; 
                MulMode.floatValue = 0; 
                break;
            case 1: // Additive Blend
                material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.One);
                AddMode.floatValue = 1; 
                MulMode.floatValue = 0; 
                break;
            case 2: // Mul
                material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.DstColor);
                material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
                AddMode.floatValue = 0; 
                MulMode.floatValue = 1; 
                break;
        }
        
        

        // 更新渲染面
        switch ((int)renderFace.floatValue)
        {
            case 0: // Back
                renderFace.floatValue = 0; 
                break;
            case 1: // Front
                renderFace.floatValue = 1;
                break;
            case 2: // Both
                renderFace.floatValue = 2;
                break;
        }

    }

    // 绘制特效功能行
    private void DrawEffectRow()
    {
        using (new EditorGUILayout.HorizontalScope(effectRowStyle))
        {
            showMixOptions = GUILayout.Toggle(showMixOptions, "混合模式", effectButtonStyle);
            showMaskOptions = GUILayout.Toggle(showMaskOptions, "遮罩模式", effectButtonStyle);
            showDistortOptions = GUILayout.Toggle(showDistortOptions, "扭曲效果", effectButtonStyle);
            showFresnelOptions = GUILayout.Toggle(showFresnelOptions, "菲涅尔效果", effectButtonStyle);
        }
    }
}

// 辅助类，用于查找材质属性
public static class ShaderGUIUtil
{
    public static MaterialProperty FindProperty(string name, MaterialProperty[] properties, bool required = true)
    {
        MaterialProperty property = FindPropertyInternal(name, properties);
        if (property == null && required)
        {
            throw new System.ArgumentException($"Could not find shader property '{name}'");
        }
        return property;
    }

    private static MaterialProperty FindPropertyInternal(string name, MaterialProperty[] properties)
    {
        for (int i = 0; i < properties.Length; i++)
        {
            if (properties[i].name == name)
            {
                return properties[i];
            }
        }
        return null;
    }
}